package routers

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/labstack/echo"
	"shqsoft.cn/pas3ui/base"
	"shqsoft.cn/pas3ui/cellphone"
)

type JurisdictionReq struct {
	UserCode string `json:"userCode"`
}

type JurisdictionResp struct {
	RoleType uint8 `json:"roleType"`
}

func JurisdictionRouter(c echo.Context) (err error) {
	req := c.Request()
	repReqBytes, err := ioutil.ReadAll(req.Body)
	if err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	var repReq JurisdictionReq
	if err := json.Unmarshal(repReqBytes, &repReq); err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	strSql := `select "ID"  from "T_SysUser" where "SysUserID" =?`
	row := base.MYDB.Raw(strSql, repReq.UserCode).Row()
	var userID int
	row.Scan(&userID)
	if userID == 0 {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: fmt.Sprintf("员工号%s在系统中不存在", repReq.UserCode)})
	}
	var rtype uint8
	rtype, err = cellphone.GetUserRoleType(userID)
	if err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	return c.JSON(http.StatusOK, RespData{Data: JurisdictionResp{RoleType: rtype}})
}

type ApplyReq struct {
	UserCode string `json:"userCode"` //工号
	Type     string `json:"type"`     //0：人民币对公存款，1：人民币对公贷款，2：零售存款，3：零售贷款，4：资金量
	RoleType string `json:"roleType"` //权限 | 0:分行行长1:支行行长2:客户经理
	Date     string `json:"date"`     //日期 | 2023-01-03
}
type ApplyResp struct {
	Money       float64             `json:"money"`       //钱款日均 | 664576.57(亿元)
	Type        string              `json:"type"`        //String   | 0：人民币对公存款 1：人民币对公贷款 2：零售存款3：零售存款4：资金量
	RankingList []cellphone.Ranking `json:"rankingList"` //Array    | 排行列表| 当权限等于0的时候展示分行排名
	ManagerList []cellphone.Manager `json:"managerList"` //Array    | 排行列表| 当权限等于1的时候展示客户经理排名
	DataList    cellphone.EmplData  `json:"dataList"`    //Object   | 当权限等于2的时候展示看板数据
}

type BrokenReq struct {
	UserCode string `json:"userCode"` //工号
	Type     string `json:"type"`     //0：人民币对公存款，1：人民币对公贷款，2：零售存款，3：零售贷款，4：资金量
	Date     string `json:"date"`     //日期 | 2023-01-03
}
type BrokenResp struct {
	List []cellphone.DataItem `json:"list"` //Array    | 数据List
}

func ApplyRouter(c echo.Context) (err error) {
	req := c.Request()
	repReqBytes, err := ioutil.ReadAll(req.Body)
	if err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	var repReq ApplyReq
	if err := json.Unmarshal(repReqBytes, &repReq); err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	strSql := `select "ID"  from "T_SysUser" where "SysUserID" =?`
	row := base.MYDB.Raw(strSql, repReq.UserCode).Row()
	var userID int
	row.Scan(&userID)
	if userID == 0 {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: fmt.Sprintf("员工号%s在系统中不存在", repReq.UserCode)})
	}
	resp := ApplyResp{Type: repReq.Type}
	resp.Money, err = cellphone.GetApplyAvvl(repReq.Date, userID, repReq.Type, repReq.RoleType)
	if err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	if repReq.RoleType == "0" {
		resp.RankingList, err = cellphone.GetApplyRankingList(repReq.Date, repReq.Type)
		if err != nil {
			return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
		}
	} else if repReq.RoleType == "1" {
		resp.ManagerList, err = cellphone.GetManagerList(repReq.Date, userID, repReq.Type)
		if err != nil {
			return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
		}
	} else {
		resp.DataList, err = cellphone.GetEmplData(repReq.Date, userID, repReq.Type)
		if err != nil {
			return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
		}
	}
	return c.JSON(http.StatusOK, RespData{Data: resp})
}

func BrokenRouter(c echo.Context) (err error) {
	req := c.Request()
	repReqBytes, err := ioutil.ReadAll(req.Body)
	if err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	var repReq BrokenReq
	if err := json.Unmarshal(repReqBytes, &repReq); err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	strSql := `select "ID" from "T_SysUser" where "SysUserID" =?`
	row := base.MYDB.Raw(strSql, repReq.UserCode).Row()
	var userID int
	row.Scan(&userID)
	if userID == 0 {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: fmt.Sprintf("员工号%s在系统中不存在", repReq.UserCode)})
	}
	var dis []cellphone.DataItem
	dis, err = cellphone.GetBrokenData(userID, repReq.Type, repReq.Date)
	if err != nil {
		return c.JSON(http.StatusOK, RespData{Code: 1, Message: err.Error()})
	}
	return c.JSON(http.StatusOK, RespData{Data: BrokenResp{List: dis}})
}
